<template>
  <header class="header-navigation">
    <!-- 左侧 Logo 和标题 -->
    <div class="header-left">
      <div class="logo" v-if="logoSrc">
        <img :src="logoSrc" :alt="title" />
      </div>
      <h1 class="title">{{ title }}</h1>
    </div>

    <!-- 中间导航链接 -->
    <nav class="header-center">
      <ul class="nav-links">
        <li
          v-for="(link, index) in navigationLinks"
          :key="index"
          class="nav-item"
        >
          <router-link
            :to="link.path"
            class="nav-link"
            :class="{ active: currentRoute === link.path }"
          >
            {{ link.label }}
          </router-link>
        </li>
      </ul>
    </nav>

    <!-- 右侧用户菜单 -->
    <div class="header-right">
      <div class="user-menu" @click="toggleUserMenu">
        <div class="user-info" v-if="userInfo">
          <div class="user-avatar" v-if="userInfo.avatar">
            <img :src="userInfo.avatar" :alt="userInfo.name" />
          </div>
          <span class="user-name">{{ userInfo.name }}</span>
        </div>
        <div class="user-icon" v-else>
          <span class="user-circle">👤</span>
        </div>
        <span class="menu-arrow" :class="{ rotated: userMenuOpen }">▼</span>
      </div>
      
      <!-- 用户下拉菜单 -->
      <div class="dropdown-menu" v-if="userMenuOpen">
        <ul>
          <li
            v-for="(item, index) in userMenuItems"
            :key="index"
            class="dropdown-item"
            @click="handleMenuItemClick(item)"
          >
            {{ item.label }}
          </li>
        </ul>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue';
import { useRoute } from 'vue-router';

// Props
interface NavigationLink {
  label: string;
  path: string;
}

interface UserInfo {
  name: string;
  avatar?: string;
}

interface UserMenuItem {
  label: string;
  action: () => void;
}

interface Props {
  title: string;
  logoSrc?: string;
  navigationLinks: NavigationLink[];
  userInfo?: UserInfo;
  userMenuItems?: UserMenuItem[];
}

const props = withDefaults(defineProps<Props>(), {
  title: '应用名称',
  logoSrc: '',
  navigationLinks: () => [
    { label: '首页', path: '/' },
    { label: '关于', path: '/about' },
    { label: '联系我们', path: '/contact' },
  ],
  userInfo: () => ({
    name: '用户',
  }),
  userMenuItems: () => [
    { label: '个人资料', action: () => console.log('个人资料') },
    { label: '设置', action: () => console.log('设置') },
    { label: '退出登录', action: () => console.log('退出登录') },
  ],
});

// 响应式数据
const userMenuOpen = ref(false);
const route = useRoute();

// 计算属性
const currentRoute = computed(() => route.path);

// 方法
const toggleUserMenu = () => {
  userMenuOpen.value = !userMenuOpen.value;
};

const handleMenuItemClick = (item: UserMenuItem) => {
  item.action();
  userMenuOpen.value = false;
};

const handleClickOutside = (event: MouseEvent) => {
  const target = event.target as HTMLElement;
  if (!target.closest('.header-right')) {
    userMenuOpen.value = false;
  }
};

// 生命周期
onMounted(() => {
  document.addEventListener('click', handleClickOutside);
});

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside);
});
</script>

<style scoped>
.header-navigation {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 64px;
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

/* 左侧 Logo 和标题 */
.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo img {
  height: 40px;
  width: auto;
  object-fit: contain;
}

.title {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  color: #1a1a1a;
}

/* 中间导航链接 */
.header-center {
  display: flex;
  align-items: center;
}

.nav-links {
  display: flex;
  gap: 24px;
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav-item {
  position: relative;
}

.nav-link {
  text-decoration: none;
  color: #666666;
  font-size: 16px;
  padding: 8px 0;
  transition: color 0.2s ease;
  position: relative;
}

.nav-link:hover {
  color: #1a1a1a;
}

.nav-link.active {
  color: #1890ff;
  font-weight: 500;
}

.nav-link.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #1890ff;
  border-radius: 1px;
}

/* 右侧用户菜单 */
.header-right {
  position: relative;
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.user-menu:hover {
  background-color: #f5f5f5;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-avatar img {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.user-name {
  font-size: 14px;
  color: #333333;
  font-weight: 500;
}

.user-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.user-circle {
  font-size: 24px;
}

.menu-arrow {
  font-size: 12px;
  transition: transform 0.2s ease;
}

.menu-arrow.rotated {
  transform: rotate(180deg);
}

/* 下拉菜单 */
.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 4px;
  background-color: #ffffff;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  min-width: 160px;
}

.dropdown-menu ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.dropdown-item {
  padding: 10px 16px;
  font-size: 14px;
  color: #333333;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.dropdown-item:hover {
  background-color: #f5f5f5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-navigation {
    padding: 0 16px;
  }

  .header-center {
    display: none;
  }

  .title {
    font-size: 18px;
  }

  .user-name {
    display: none;
  }
}
</style>